package cn.edu.sdjzu.xg23.kcsj01_backend.service.impl;

import cn.edu.sdjzu.xg23.kcsj01_backend.entity.ParticipatingConference;
import cn.edu.sdjzu.xg23.kcsj01_backend.mapper.AbsAchievementMapper;
import cn.edu.sdjzu.xg23.kcsj01_backend.mapper.ParticipatingConferenceMapper;
import cn.edu.sdjzu.xg23.kcsj01_backend.utils.SnowflakeIdGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ParticipatingConferenceServiceImpl {

    @Autowired
    private ParticipatingConferenceMapper participatingConferenceMapper;

    @Autowired
    private AbsAchievementMapper absAchievementMapper;

    @Autowired
    private SnowflakeIdGenerator idGenerator;

    public boolean addParticipatingConference(ParticipatingConference participatingConference) {
        long sharedId = idGenerator.generateId();

        // 1. 插入Achievement记录
        absAchievementMapper.addAchievement(sharedId, "参会");

        participatingConference.setId(sharedId);
        return participatingConferenceMapper.addParticipatingConference(participatingConference);
    }

    public boolean deleteParticipatingConference(Long participatingConferenceId) {
        return participatingConferenceMapper.deleteParticipatingConference(participatingConferenceId);
    }

    public boolean updateParticipatingConference(ParticipatingConference participatingConference) {
        try {
            return participatingConferenceMapper.updateParticipatingConference(participatingConference);
        } catch (Exception e) {
            throw new RuntimeException("更新参会记录失败", e);
        }
    }

    public PageInfo<ParticipatingConference> selectParticipatingConferencesByCondition(int pageNum, int pageSize, ParticipatingConference participatingConference) {
        PageHelper.startPage(pageNum, pageSize);
        List<ParticipatingConference> list = participatingConferenceMapper.selectParticipatingConferencesByCondition(participatingConference);
        return PageInfo.of(list);
    }

    public boolean approvalParticipatingConference(Long id, Long approvalCode) {
        try {
            return participatingConferenceMapper.approvalParticipatingConference(id, approvalCode);
        } catch (Exception e) {
            throw new RuntimeException("审批操作失败", e);
        }
    }
}